package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IZhikeGroupBaseService;
import com.mdd.admin.validate.UpdateValidate;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ZhikeGroupBaseCreateValidate;
import com.mdd.admin.validate.ZhikeGroupBaseUpdateValidate;
import com.mdd.admin.validate.ZhikeGroupBaseSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.user.UserUpdateValidate;
import com.mdd.admin.vo.ZhikeGroupBaseListedVo;
import com.mdd.admin.vo.ZhikeGroupBaseDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import com.zhike.common.dto.GroupMember4Cache;
import com.zhike.service.group.IGroupMemberService;
import com.zhike.service.group.IGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/base")
@Api(tags = "群组基本信息管理")
public class ZhikeGroupBaseController {

    @Resource
    IZhikeGroupBaseService iZhikeGroupBaseService;

    @Autowired
    IGroupMemberService groupMemberService;

    @GetMapping("/list")
    @ApiOperation(value="群组基本信息列表")
    public AjaxResult<PageResult<ZhikeGroupBaseListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated ZhikeGroupBaseSearchValidate searchValidate) {
        PageResult<ZhikeGroupBaseListedVo> list = iZhikeGroupBaseService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/mermberList")
    @ApiOperation(value = "获取群成员列表")
    public AjaxResult<List<GroupMember4Cache>> getMermberList(String gid){
        List<GroupMember4Cache> groupMember4Caches = groupMemberService.queryGroupMemberList(gid);
        return AjaxResult.success(groupMember4Caches);
    }

    @GetMapping("/detail")
    @ApiOperation(value="群组基本信息详情")
    public AjaxResult<ZhikeGroupBaseDetailVo> detail(@Validated @IDMust() @RequestParam("id") String id) {
        ZhikeGroupBaseDetailVo detail = iZhikeGroupBaseService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "群组基本信息新增")
    @PostMapping("/add")
    @ApiOperation(value="群组基本信息新增")
    public AjaxResult<Object> add( @RequestBody ZhikeGroupBaseCreateValidate createValidate) {
        iZhikeGroupBaseService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "群组基本信息编辑")
    @PostMapping("/edit")
    @ApiOperation(value="群组基本信息编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody UpdateValidate updateValidate) {
        iZhikeGroupBaseService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "群组基本信息删除")
    @PostMapping("/del")
    @ApiOperation(value="群组基本信息删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iZhikeGroupBaseService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
